package config

import (
	"gopkg.in/yaml.v2"
	"io/ioutil"
	"log"
)

type Config struct {
	//服务器相关配置
	Server struct {
		AppId  string `yml:"appid"`
		Port   string `yml:"port"`
		Banner struct {
			Name    string `yml:"name"`
			Loading bool   `yml:"loading"`
		} `yml:"banner"`
	} `yml:"server"`
	//数据库相关配置
	Data struct {
		DataType string `yml:"data-type"`
		DataBase string `yml:"database"`
		Url      string `yml:"url"`
		Username string `yml:"username"`
		Password string `yml:"password"`
		Init     struct {
			Name   string `yml:"name"`
			Status bool   `yml:"status"`
		} `yml:"init"`
		Test struct {
			Name   string `yml:"name"`
			Status bool   `yml:"status"`
		} `yml:"test"`
	} `yml:"data"`
	//自定义相关配置
	My struct {
		LogUrl string `yml:"logurl"`
	} `yml:"my"`
}

func NewConfig() *Config {
	return &Config{}
}
func (c *Config) ReadConfig() {
	file, err := ioutil.ReadFile("application.yml")
	if err != nil {
		log.Fatalln("读取文件application.yml发生错误")
		return
	}
	if yaml.Unmarshal(file, c) != nil {
		log.Fatalln("解析文件application.yml发生错误")
		return
	}
}

var C *Config

func init() {
	C = NewConfig()
	C.ReadConfig()
}
